<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="运营大区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysOrgCode">
              <a-tree-select
                v-model="model.sysOrgCode"
                :tree-data="deptTreeDict"
                :replaceFields="replaceFields"
                :show-checked-strategy="SHOW_PARENT"
                search-placeholder="请选择运营大区"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="线路名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入线路名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="起点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startSpotId">
              <a-select
                v-model="model.startSpotId"
                placeholder="请选择起点景点"
                style="width: 100%"
              >
                <a-select-option v-for="(item, index)  in scenicSpotOptions" :key="index.key" :value="item.value">{{item.label}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="终点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endSpotId">
              <a-select
                v-model="model.endSpotId"
                placeholder="请选择终点景点"
                style="width: 100%"
              >
                <a-select-option v-for="(item, index)  in scenicSpotOptions" :key="index.key" :value="item.value">{{item.label}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="途径景点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="spotIdList">
              <a-select
                mode="multiple"
                v-model="model.spotIdList"
                placeholder="请选择途径景点"
                style="width: 100%"
              >
                <a-select-option v-for="(item, index)  in scenicSpotOptions" :key="index.id" :value="item.id">{{item.name}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="路径" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="spotIdList">
              <PlanRoute :waypoints="waypoints"  :polyline.sync="model.polyline" ></PlanRoute>
            </a-form-model-item>
          </a-col>
          
          <a-col :span="24">
            <a-form-model-item label="简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="intro">
              <a-textarea v-model="model.intro" rows="4" placeholder="请输入简介" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { TreeSelect } from 'ant-design-vue';
  const SHOW_PARENT = TreeSelect.SHOW_PARENT;
  const replaceFields =	{children:'children', title:'title', key:'key', value: 'orgCode' };
  // import PlanRoute from "@/components/map/tencent/PlanRoute"
  import SearchLocation from "@/components/map/dialog/SearchLocation"
  import _ from 'lodash';
  export default {
    name: 'RentalScenicSpotRouteForm',
    components: {
      // PlanRoute,
      SearchLocation,
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           name: [
              { required: true, message: '请输入线路名称!'},
           ],
           startLatitude: [
              { required: true, message: '请输入起点维度!'},
           ],
           startLongitude: [
              { required: true, message: '请输入起点经度!'},
           ],
           endLatitude: [
              { required: true, message: '请输入终点维度!'},
           ],
           endLongitude: [
              { required: true, message: '请输入终点经度!'},
           ],
        },
        url: {
          add: "/rental/scenicspot-route/add",
          edit: "/rental/scenicspot-route/edit",
          queryById: "/rental/scenicspot-route/queryById",
          queryTreeUrl: "/sys/sysDepart/queryTreeList",
          scenicSpotOptionsUrl: "/rental/scenicspot/options"
        },
        deptTreeDict:[],
        SHOW_PARENT,
        replaceFields,
        scenicSpotOptions:[],
        waypoints:[],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    watch: {
      "model.spotIdList":{
        handler(val,oldVal){
          console.log("watch----------spotIdList:", val,oldVal);
          if(val && !_.isEqual(val, oldVal) && this.scenicSpotOptions.length > 0){
            console.log("watch----------spotIdList:", val,oldVal,this.scenicSpotOptions);
            this.waypoints = [];
            for(let i=0;i<val.length;i++){
              let index = this.scenicSpotOptions.findIndex(item => item.id == val[i]);
              if(index != -1){
                this.waypoints.push(this.scenicSpotOptions[index]);
              }
            }
            console.log("this.waypoints:", this.waypoints);
          }
        },
        deep: true,
      },
      "scenicSpotOptions":{
        handler(val,oldVal){
          if(val && val.length > 0){
            console.log("watch----------scenicSpotOptions:", val,oldVal,this.model.spotIdList);
            this.waypoints = [];
            if(this.model.spotIdList){
              for(let i=0;i<this.model.spotIdList.length;i++){
                let index = val.findIndex(item => item.id == this.model.spotIdList[i]);
                if(index != -1){
                  this.waypoints.push(val[index]);
                }
              }
              console.log("this.waypoints:", this.waypoints);
            }
          }
        },
        deep: true,
      },
    },
    created () {
      this.getScenicSpotOptions();
      this.getDeptTreeDict();
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      getScenicSpotOptions(){
        getAction(this.url.scenicSpotOptionsUrl,{}).then((res)=>{
          this.scenicSpotOptions=res.result;
        }).finally(() => {

        })
      },
      getDeptTreeDict(){
        getAction(this.url.queryTreeUrl,{}).then((res)=>{
          this.deptTreeDict=res.result;
        }).finally(() => {

        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>